﻿using System;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;


namespace ASPNETVN.PORTAL.Modules.POLL.Components
{
    public partial class Answer
    {
        /// <summary>
        /// Select answers of questionID
        /// </summary>
        /// <param name="questionID"></param>
        /// <returns></returns>
        public static DataSet SelectBy_QuestionID(int questionID)
        {
            string spName = "p_POLL_Answer_SelectBy_QuestionID";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@QuestionID", DbType.Int32, questionID);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// Select answer, question of moduleID with specific status
        /// </summary>
        /// <param name="moduleID"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static DataSet SelectBy_ModuleID_Status(Guid moduleID, bool status)
        {
            string spName = "p_POLL_Answer_SelectBy_ModuleID_Status";
            SqlDatabase db = DatabaseFactory.CreateDatabase() as SqlDatabase;
            SqlCommand dbCommand = db.GetStoredProcCommand(spName) as SqlCommand;
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@Status", SqlDbType.Bit, status);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// Increase vote
        /// </summary>
        /// <param name="answerID"></param>
        /// <returns></returns>
        public static int IncreasePoll(int answerID)
        {
            string spName = "p_POLL_Answer_IncreasePoll";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@AnswerID", DbType.Int32, answerID);
            return db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Update description of questionID
        /// </summary>
        /// <param name="id"></param>
        /// <param name="des"></param>
        /// <returns></returns>
        public static int UpdateDes(int id, string des)
        {
            string spName = "[dbo].[p_POLL_Answer_Update_Description]";
            SqlDatabase db = DatabaseFactory.CreateDatabase() as SqlDatabase;
            SqlCommand dbCommand = db.GetStoredProcCommand(spName) as SqlCommand;
            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, id);
            db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, des);
            return db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Get result of questionID
        /// </summary>
        /// <param name="questionID"></param>
        /// <returns></returns>
        public static DataSet SelectResultBy_QuestionID(int questionID)
        {
            string spName = "p_POLL_Answer_SelectResultBy_QuestionID";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@QuestionID", DbType.Int32, questionID);
            return db.ExecuteDataSet(dbCommand);
        }
    }
}
